package com.ysh.xiaomi.web.servelt;

import com.ysh.xiaomi.pojo.TbAddress;
import com.ysh.xiaomi.pojo.TbUser;
import com.ysh.xiaomi.service.AddressService;
import com.ysh.xiaomi.service.impl.AddressServiceImpl;
import com.ysh.xiaomi.utils.StringConstans;
import org.apache.commons.beanutils.BeanUtils;

import javax.mail.Address;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 袁胜虎 on 2022/8/20 21:09
 */
@WebServlet("/address")

public class AddressServlet extends BaseServlet{
    AddressService addressService = new AddressServiceImpl();


public String show(HttpServletRequest request, HttpServletResponse response){
    TbUser user = (TbUser) request.getSession().getAttribute("user");
    if (user == null) {
        return StringConstans.REDIRECT + "/login.jsp";
    }
    Long uid = user.getId();
    List<TbAddress> addressList = addressService.findAllAddressByUserId(uid);
    request.getSession().setAttribute("list",addressList);
    return StringConstans.REDIRECT +"/self_info.jsp";
  }
    public String add(HttpServletRequest req, HttpServletResponse response){

        String name =  req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");
        TbUser user = (TbUser) req.getSession().getAttribute("user");
        long uid = user.getId();

        TbAddress address = new TbAddress();
        address.setDetail(detail);
        address.setUid(uid);
        address.setName(name);
        address.setPhone(phone);
        addressService.saveAddress(address);
        return StringConstans.FORWARD + "/address?method=show";
    }
    public String delete(HttpServletRequest req, HttpServletResponse response){
        String aid = req.getParameter("id");
        addressService.deleteAddressByAid(aid);
        return StringConstans.FORWARD + "/address?method=show";
    }
}
